package com.vnp.cooking;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.vnp.cooking.bean.ObjCookpad;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class CommentFBActivity extends Activity implements OnClickListener{
	
	WebView webComment;
	ProgressBar progressBar;
	ImageButton btnBack, btnRefresh;
	
	ObjCookpad objCookpad = new ObjCookpad();
	
	String BASE_URL = "http://";
	EasyTracker easyTracker;
	private static final String SCREEN_LABEL = "Binh luan screen";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_fb);
		easyTracker = EasyTracker.getInstance(this);
		
		objCookpad = getIntent().getParcelableExtra("objCookpad");
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnRefresh = (ImageButton) findViewById(R.id.btn_refresh);
		webComment = (WebView) findViewById(R.id.webComment);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		
		loadComment();
		
		init();
		ApplicationContext.getGaTracker().set(Fields.SCREEN_NAME, SCREEN_LABEL);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		ApplicationContext.getGaTracker().send(MapBuilder.createAppView().build());
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	
	void init() {
		btnBack.setOnClickListener(this);
		btnRefresh.setOnClickListener(this);
	}
	
	void loadComment() {
		webComment.getSettings().setJavaScriptEnabled(true);
		webComment.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100) {
                	progressBar.setVisibility(ProgressBar.INVISIBLE);
                    progressBar.setProgress(0);
                } else {
                	progressBar.setVisibility(ProgressBar.VISIBLE);
                }
            }
        });
		
		webComment.loadUrl(BASE_URL+objCookpad.getUrlComment());
		
		easyTracker.send(MapBuilder.createEvent("CONGTHUC", "COMMENT", "BINH LUAN CONG THUC", null).build());
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			closeActivity();
			break;
		case R.id.btn_refresh:
			webComment.stopLoading();
			webComment.clearHistory();
			loadComment();
			break;
		default:
			break;
		}
	}
	
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		closeActivity();
	}


}
